//
//  GameViewController.m
//  2048
//
//  Created by 高新川 on 15/6/7.
//  Copyright (c) 2015年 高新川. All rights reserved.
//

#import "GameViewController.h"
#import "UIColor+Hex.h"
#import "MainView.h"
#import "SettingViewController.h"
#import "GameViewFactory.h"
#import "GameViewObject.h"
#import "Game.h"

@interface GameViewController ()

@end

@implementation GameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"FFF8DC"];
    [self initGameView];
    [self changeGameView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingBack:) name:SETTING_BACK object:nil];
}

- (void)didReceiveMemoryWarning {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SETTING_BACK object:nil];
    
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - initView

/*
 * 设置游戏界面的显示
 **/
- (void)initGameView {
    
    // 设置2048大标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, 25, 110, 110)];
    title.layer.cornerRadius = 10.0;
    title.text = @"2048";
    title.textAlignment = NSTextAlignmentCenter;
    title.backgroundColor = [UIColor colorWithHexString:@"F4A460"];
    title.textColor = [UIColor colorWithHexString:@"8F8F8F"];
    title.font  = [UIFont systemFontOfSize:40 weight:15];
    [self.view addSubview:title];
    
    // 设置标题下边的文字显示
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 145, 290, 30)];
    line1.text = @"Join the numbers and get the 2048!";
    line1.textColor = [UIColor colorWithHexString:@"8F8F8F"];
    line1.font  = [UIFont systemFontOfSize:15 weight:15];
    [self.view addSubview:line1];
    
    // 设置左上角的两个积分标题
    _scoreLabel = [[MainView alloc] initWithFrame:CGRectMake(160, 25, 70, 70)];
    [_scoreLabel setTitle:@"SCORE" andScore:@"0"];
    [self.view addSubview:_scoreLabel];

    _highLabel = [[MainView alloc] initWithFrame:CGRectMake(235, 25, 70, 70)];
    [_highLabel setTitle:@"HIGH" andScore:@"0"];
    [self.view addSubview:_highLabel];
    
    // 设置积分标题下的两个按钮
    UIButton *set = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    set.frame = CGRectMake(160, 105, 70, 30);
    set.layer.cornerRadius = 10.0;
    [set setTitle:@"设置" forState:UIControlStateNormal];
    set.backgroundColor = [UIColor colorWithHexString:@"FF8247"];
    [set addTarget:self action:@selector(gameSetting) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:set];
    
    UIButton *gameNew = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    gameNew.frame = CGRectMake(235, 105, 70, 30);
    gameNew.layer.cornerRadius = 10.0;
    [gameNew setTitle:@"重新开始" forState:UIControlStateNormal];
    gameNew.backgroundColor = [UIColor colorWithHexString:@"FF8247"];
    [gameNew addTarget:self action:@selector(gameNew) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:gameNew];
}

#pragma mark - button action
// 点击设置按钮
- (void)gameSetting {
    SettingViewController *game = [[SettingViewController alloc] init];
    //[self presentModalViewController:game animated:YES completion:nil];
    
    [self presentViewController:game animated:YES completion:nil];
}

//  点击重新开始
- (void)gameNew {
    [self changeGameView];
}

// 通知：设置完成
- (void)settingBack:(NSNotification *)notification {
    NSString *type = notification.object;
    if ([type isEqualToString:@"cancel"]) {
        //
    }
    else {
        [self changeGameView];
    }
    NSLog(@"%@", type);
}

// 手势下滑
- (void)handleSwipeBottom {
    [[Game getInstance] bottom];                // 游戏类下移操作
    int add = [Game getInstance].addScore;      // 获得家分数
    [self updataScore:add];                     // 更新左上角俩记分的label
    [self updataView];                          // 更新n*n游戏格子
}

// 手势上滑
- (void)handleSwipeTop {
    [[Game getInstance] top];
    int add = [Game getInstance].addScore;
    [self updataScore:add];
    [self updataView];
}

// 手势左滑
- (void)handleSwipeLeft {
    [[Game getInstance] left];
    int add = [Game getInstance].addScore;
    [self updataScore:add];
    [self updataView];
}

// 手势右滑
- (void)handleSwipeRight {
    [[Game getInstance] right];
    int add = [Game getInstance].addScore;
    [self updataScore:add];
    [self updataView];
}

#pragma mark - 更改游戏界面

/*
 * 重设记分label，重设n*n游戏空格
 **/
- (void)changeGameView {
    _scoreLabel.nowScore = 0;
    [_scoreLabel showScore:0];
    [[Game getInstance] initGame];
    
    int matrix = [Game getInstance].model.matrix;
    self.gameView = [GameViewFactory createGameView:matrix];
    if (_gameView) {
        _gameView.frame = CGRectMake(15, 180, 290, 290);
        _gameView.backgroundColor = [UIColor colorWithHexString:@"B5B5B5"];
        _gameView.layer.cornerRadius = 10.0;
        
        [_gameView initViews];
        [_gameView showNumbers];
        [self.view addSubview:_gameView];
        
        [self bindAction];
    }
}

/*
 * 绑定手势事件，上下左右
 **/
- (void)bindAction {
    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [self.gameView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.gameView addGestureRecognizer:recognizer];
   
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeTop)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self.gameView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeBottom)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [self.gameView addGestureRecognizer:recognizer];
}

/*
 * 重新显示n*n游戏数据
 **/
- (void)updataView {
    [_gameView showNumbers];
}

/*
 * 更新记分项
 **/
- (void)updataScore:(int)addScore {
    int add = [Game getInstance].addScore;
    [_scoreLabel showScore:add];
    int high = _scoreLabel.nowScore;
    [_highLabel showHigh:high];
}

@end
